jQuery(document).ready(function() {
	if(jQuery('.checkall').length > 0) {
		jQuery('.checkall').click(function(){
			var parentTable = jQuery(this).parents('table');										   
			var ch = parentTable.find('tbody input[type=checkbox]');										 

			if(jQuery(this).is(':checked')) {
				ch.each(function(){
					if(!jQuery(this).is(':checked')) {
						jQuery(this).trigger('click');
					}
				});
			} else {
				ch.each(function(){
					if(jQuery(this).is(':checked')) {
						jQuery(this).trigger('click');
					}
				});	
			}
		});
	}

	jQuery('tbody .NS-Checkbox').on('click', function() {
		var isChecked = jQuery(this).is(':checked');

		if(isChecked) {
			jQuery(this).parents('tr').addClass('selected');
		} else {
			jQuery(this).parents('tr').removeClass('selected');
		}
	});
});

function confirmDelete(message_not_selected, message_confirmation, delete_url) {
	if(jQuery('.application_id:checked').length == 0) {
		jQuery('#popup-delete').text(message_not_selected).dialog({
			title: 'Message',
			resizable: false,
			modal: true,
			buttons: {
				'OK': function() {
					jQuery(this).dialog('close');
				}	
			}
		});
	} else {
		jQuery('#popup-delete').text(message_confirmation.format(jQuery('.application_id:checked').length)).dialog({
			title: 'Confirmation',
			resizable: false,
			modal: true,
			buttons: {
				'Delete': function() {
					jQuery(this).dialog('close');
					jQuery('form').attr('action', delete_url).submit();
				},
				'Cancel': function() {
					jQuery(this).dialog( 'close' );
				}	
			}
		});
	}
}

if (!String.prototype.format) {
	String.prototype.format = function() {
		var args = arguments;
	    
		return this.replace(/{(\d+)}/g, function(match, number) {
			return typeof args[number] != 'undefined' ? args[number] : match;
		});
	};
}